package com.zxh.servlet;

import com.zxh.pojo.User;
import com.zxh.service.UserServiceImpl;
import com.zxh.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
//    servlet:控制层，调用 业务代码
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req,resp);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("========loginServlet start===========");
        String userCode = req.getParameter("userCode");
        String password = req.getParameter("userPassword");
        UserServiceImpl userService = new UserServiceImpl();
        User us = userService.login(userCode,password);
        if (null != us){
          req.getSession().setAttribute(Constants.USER_SESSION,us);
          resp.sendRedirect("jsp/frame.jsp");
        }else{
            req.setAttribute("error", "用户名或密码错误！！！！！");
            req.getRequestDispatcher("login.jsp").forward(req, resp);

        }
        System.out.println("========loginServlet finish===========");

    }

}
